﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TestJQGrid
{
    public partial class RuleExecution : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_dlRuleList();

                lblruleName.Text = "";
                lblruleType.Text = "";

                lblMenuName.Text = "RULE EXECUTION";
                lblMenuName.Font.Bold = true;
                lblMenuName.Font.Size = 14;
                lblMenuName.ForeColor = System.Drawing.Color.Navy;
            }
        }

        private void fill_dlRuleList()
        {
            dlFormula.DataSource = GlobalMethods.ExecuteSelectQuery("select RuleName,RuleID from RuleDetails r where r.ToDate > '" + DateTime.Now.ToString() + "'");
            dlFormula.DataBind();
        }

        private void fill_Subrule(int RuleId)
        {
            lb_Subrule.DataSource = GlobalMethods.ExecuteSelectQuery("select FormulaTable.ID, FormulaTable.FormulaName from FormulaTable, RuleRelations where RuleRelations.SubRuleId = FormulaTable.ID AND RuleRelations.RuleId = '" + RuleId.ToString() + "' ORDER BY RuleRelations.Priority");
            lb_Subrule.DataTextField = "FormulaName";
            lb_Subrule.DataValueField = "ID";
            lb_Subrule.DataBind();

            lb_Subrule.Width = 250;
        }

        private void fill_lbUnbilledRule(int RuleId)
        {
            rbl_cycle.DataSource = GlobalMethods.ExecuteSelectQuery("select CycleId, (CONVERT(varchar,StartDate,06) + ' - ' +CONVERT(varchar,EndDate,06)) as cycle from CycleStatus where Status not in ('Billed') AND RuleId = '" + RuleId.ToString() + "'");
            rbl_cycle.DataTextField = "cycle";
            rbl_cycle.DataValueField = "CycleID";
            rbl_cycle.DataBind();

            rbl_cycle.SelectedIndex = 0;
            rbl_cycle.Width = 200;
        }

        protected void lbt_formula_Click(object sender, EventArgs e)
        {
            int rid = 0;
            LinkButton lbtn = sender as LinkButton;

            if (lbtn != null)
            {
                rid = Convert.ToInt32(lbtn.CommandArgument);
                ShowRuleDetails(rid);
            }
        }

        private void ShowRuleDetails(int rid)
        {
            DataTable dt = new DataTable();

            hf_ruleID.Value = rid.ToString();

            dt = GlobalMethods.ExecuteSelectQuery("select RuleName,RuleID,RuleType from RuleDetails r where r.RuleID=" + rid);
            if (dt != null && dt.Rows.Count > 0)
            {
                lblruleName.Text = dt.Rows[0]["RuleName"].ToString();
                lblruleType.Text = dt.Rows[0]["RuleID"].ToString();
                hf_ruleType.Value = dt.Rows[0]["RuleType"].ToString();
            }

            fill_Subrule(rid);
            fill_lbUnbilledRule(rid);
        }

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            lblruleName.Text = "";
            lblruleType.Text = "";

            hf_ruleID.Value = "";
            hf_ruleType.Value = "";
        }

        protected void btn_execute_Click(object sender, EventArgs e)
        {
            int cycleid = Convert.ToInt32(rbl_cycle.SelectedValue);
            int RuleID = Convert.ToInt32(hf_ruleID.Value);
            string RuleType = hf_ruleType.Value;

            if (!string.IsNullOrEmpty(rbl_cycle.SelectedValue))
            {
                //ddl.insertQuery("update CycleStatus set Status='Billed' where RuleID=" + RuleID + " and CycleID=" + cycleid);
                //ddl.insertQuery("insert into RulePayout(RuleId,CycleId,AgentId,RuleType,Comission,CreatedBy,CreatedOn) values(" + RuleID + "," + cycleid + ",'Agent','" + RuleType + "',0,'" + Session["loginId"].ToString() + "','" + DateTime.Now.ToString() + "')");
                //WebMsgBox.Show("Record executed succesfully.");
                //Response.Redirect("RuleExecutionResult.aspx?RuleID=" + RuleID + "&CycleID=" + cycleid);
                Response.Redirect("RuleExecutionResult2.aspx?RuleID=1&CycleID=4");
            }
        }
    }
}